// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 15:15:17
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Load\MOMENT2Parser.cpp
 */
#include "Import/BDFImport/BulkData/Load/MOMENT2Parser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/IMOMENT2Service.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (MOMENT2);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/MOMENT2/MOMENT2.md>
*/
// 以下需要修改
enum Moment2Key : std::uint8_t
{
    Moment2KEY_SID    = 2,  // 荷载组标识号
    Moment2KEY_nodeID = 3,  // 节点标识号
    Moment2KEY_force  = 4,  // 集中力大小
    Moment2KEY_G1     = 5,  // 确定Moment2的四个节点号
    Moment2KEY_G2     = 6,  // 确定Moment2的四个节点号
    Moment2KEY_G3     = 7,  // 确定Moment2的四个节点号
    Moment2KEY_G4     = 8,  // 确定Moment2的四个节点号
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseMOMENT2 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                               std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建一个用于保存MOMENT2数据的临时结构体对象
    SG::DataStructure::FEM::FORCE2Data tempMoment2;

    // TODO(bug 001) 填写m_domainId的正确值
    // 目前m_domainId硬编码为1，需要进一步确认是否正确
    tempMoment2.m_domainId = 1;

    // 初始化当前处理的字符串条目的ID
    int curIDKey = 1;

    // 遍历所有分割后的条目信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 判断当前条目是否为空（即是否全为空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            // 如果当前条目为空，跳过当前循环并处理下一个条目
            curIDKey++;
            continue;
        }

                // 根据条目ID，将解析后的数值存入对应的MOMENT2字段
        switch (curIDKey)
        {
        case Moment2KEY_SID:
            // 解析SID并赋值
            tempMoment2.m_sId = std::stoul (curStrKey);
            break;
        case Moment2KEY_nodeID:
            // 解析节点ID并赋值
            tempMoment2.m_nodeId = std::stoul (curStrKey);
            break;
        case Moment2KEY_force:
            // 解析力矩的值并赋值
            tempMoment2.m_force = convertToReal (curStrKey);
            break;
        case Moment2KEY_G1:
            // 解析第一个gi值并赋值
            tempMoment2.m_gi[0] = std::stoul (curStrKey);
            break;
        case Moment2KEY_G2:
            // 解析第二个gi值并赋值
            tempMoment2.m_gi[1] = std::stoul (curStrKey);
            break;
        case Moment2KEY_G3:
            // 解析第三个gi值并赋值
            tempMoment2.m_gi[2] = std::stoul (curStrKey);
            break;
        case Moment2KEY_G4:
            // 解析第四个gi值并赋值
            tempMoment2.m_gi[3] = std::stoul (curStrKey);
            break;
        default:
            // 如果条目ID不匹配，跳过处理
            break;
        }

        // 增加条目ID，以便处理下一个条目
        curIDKey++;
    }

    // 将解析后的MOMENT2数据通过数据库服务工厂的MOMENT2服务添加到数据库中
    return dbServiceFactory->get<IMOMENT2Service> ()->Add (tempMoment2);
}